package net.qhzw.vip.qiyu.listener;

import lombok.extern.slf4j.Slf4j;
import net.qhzw.vip.qiyu.entity.Order;
import net.qhzw.vip.qiyu.entity.OrderItem;
import net.qhzw.vip.qiyu.enums.OrderStatus;
import net.qhzw.vip.qiyu.handler.ApiException;
import net.qhzw.vip.qiyu.service.OrderItemService;
import net.qhzw.vip.qiyu.service.OrderService;
import net.qhzw.vip.qiyu.service.ProductService;
import net.qhzw.vip.qiyu.service.SkuService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;

/**
 * 监听死信队列 订单30分钟未支付自动取消
 */
@Slf4j
@Component
public class OrderDeadLetterConsumer {

    @Autowired
    private OrderService orderService;

    @Autowired
    private OrderItemService orderItemService;

    @Autowired
    private ProductService productService;

    @Autowired
    private SkuService skuService;


    /***
     * 30分钟超时未支付的取消订单
     * @param message
     */
    @RabbitListener(queues = "DEAD_LETTER_QUEUE_CANCEL_ORDER")
    public void cancel(Message message) {
        String orderNumbers = new String(message.getBody());
        log.info("当前时间{}，30分钟超时未支付自动取消订单, 订单号: ：{}", orderNumbers);
        for (String orderNumber : orderNumbers.split(",")) {
            Order order = orderService.getOrderByOrderNumber(orderNumber);
            if (order == null) {
                return;
            }
            if (order.getIsPayed() != 1) {
                List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber);
                order.setOrderItems(orderItems);
                // 取消订单
                orderService.cancelOrders(Arrays.asList(order), (byte) 6);
                // 清除缓存
                for (OrderItem orderItem : orderItems) {
                    productService.removeProductCacheByProdId(orderItem.getProdId());
                    skuService.removeSkuCacheBySkuId(orderItem.getSkuId(), orderItem.getProdId());
                }
            }
        }

    }

    /**
     * 自动收货
     *
     * @param message
     */
    @RabbitListener(queues = "DEAD_LETTER_QUEUE_RECEIPT")
    public void printMsg2(Message message) {
        String orderNumber = new String(message.getBody());
        log.info("当前时间{}, 发货后14天未收货,自动收货订单号: ：{}", orderNumber);
        Order order = orderService.getOrderByOrderNumber(orderNumber);
        if (order == null){
            return;
        }
        if (!Objects.equals(order.getStatus(), OrderStatus.CONSIGNMENT.value())) {
            //throw new ApiException("订单未发货，无法确认收货");
            return;
        }
        if (Objects.equals(order.getStatus(), OrderStatus.SUCCESS.value()) || Objects.equals(order.getStatus(), OrderStatus.CONFIRM.value())) {
           return;
        }
        List<OrderItem> orderItems = orderItemService.getOrderItemsByOrderNumber(orderNumber);
        order.setOrderItems(orderItems);
        // 确认收货
        orderService.confirmOrder(Arrays.asList(order));
        for (OrderItem orderItem : orderItems) {
            productService.removeProductCacheByProdId(orderItem.getProdId());
            skuService.removeSkuCacheBySkuId(orderItem.getSkuId(), orderItem.getProdId());
        }

    }


}
